package com.atguigu.realtime.app;

import com.atguigu.realtime.util.FlinkSourceUtil;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.runtime.state.storage.FileSystemCheckpointStorage;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @author chenlongStart
 * @create 2021-06-25 10:58
 * @desc
 */
public abstract class BaseSqlApp {

    public void init(int port, int parallelism, String ckDir){

        System.setProperty("HADOOP_USER_NAME", "atguigu");
        Configuration conf=new Configuration();
        conf.setInteger("rest.port",port);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf).setParallelism(parallelism);

        //设置状态后端
        env.setStateBackend(new HashMapStateBackend());
        env.getCheckpointConfig().setCheckpointStorage(new FileSystemCheckpointStorage("hdfs://hadoop162:8020/flink_realtime/ck/"+ckDir));

        //设置checkpoint相关参数
        //1、设置精准一次， 每5秒开始一次checkpoint
        env.enableCheckpointing(5000, CheckpointingMode.EXACTLY_ONCE);
        //2、设置checkpoint超时时间，checkpoint必须在一分钟内完成，否则就会被抛弃
        env.getCheckpointConfig().setCheckpointTimeout(60000);
        //3、开启在job中中止后仍然保留的 externalized checkpoints
        env.getCheckpointConfig().enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);

        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        run(tEnv);

        try {
            env.execute(ckDir);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    //提供抽象run()方法，以便继承后重写，不同的业务逻辑
    protected abstract void run(StreamTableEnvironment tEnv);
}
